package demo;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField;

public class Test10 {

    // 使用严格模式创建 DateTimeFormatter
    private static final DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
            .appendPattern("yyyy-MM-dd")
            .parseDefaulting(ChronoField.ERA, 1)
            .toFormatter()
            .withResolverStyle(ResolverStyle.STRICT);

    public static void main(String[] args) {
        // 测试用例
        System.out.println(validateDates("2025-09-01", "2025-09-05")); // true - 正常日期
        System.out.println(validateDates("2025-09-01", "2025-09-01")); // true - 正常日期
        System.out.println(validateDates("2025-09-03", "2025-09-02")); // false - 正常日期
        System.out.println(validateDates("2025-09-32", "2025-09-05")); // false - 无效日期（9月没有32号）
        System.out.println(validateDates("2025-02-29", "2025-03-01")); // false - 无效日期（2025不是闰年）
        System.out.println(validateDates("2025-13-01", "2025-09-01")); // false - 无效月份
        System.out.println(validateDates("2025--01", "2025-09-01")); // false - 无效月份
    }

    public static boolean validateDates(String startDate, String endDate) {
        if (startDate == null || endDate == null ||
                startDate.trim().isEmpty() || endDate.trim().isEmpty()) {
            return false;
        }

        try {
            LocalDate start = LocalDate.parse(startDate.trim(), DATE_FORMATTER);
            LocalDate end = LocalDate.parse(endDate.trim(), DATE_FORMATTER);
            return !start.isAfter(end);
        } catch (Exception e) {
            return false;
        }
    }
}